﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AUV5.Web.Handler
{
    /// <summary>
    /// ServiceHandler 的摘要说明
    /// </summary>
    public class ServiceHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //收藏和取消收藏
            if (context.Request["ServiceID"] != null && context.Request["UserID"] != null && context.Request["Command"] != null)
            {
                SetFavorite(context);
            }
            //添加增值服务
            else if (context.Request["ServiceID"] != null && context.Request["Title"] != null && context.Request["Price"] != null)
            {
                if (context.Request["Command"] == "Add")
                {
                    ExtraService(context);
                }
            }

            //删除增值服务
            else if(context.Request["serviceID"]!=null&&context.Request["Command"]!=null)
            {
                if (context.Request["Command"] == "Delete")
                {
                    Model.ExtraService service = BLL.ExtraServiceBLL.Instance.GetDetail(context.Request["serviceID"]);
                    if (service != null)
                    {
                        BLL.ExtraServiceBLL.Instance.Delete(service);
                        context.Response.Write("100");
                    }
                }
            }

        }

        //添加增值服务
        private void ExtraService(HttpContext context)
        {
            try
            {
                if (new PageBaseTop().ExtraService_Enabled)
                {
                    string serviceID = context.Request["ServiceID"];
                    string title = context.Request["Title"];
                    int price = 0;

                    if (string.IsNullOrEmpty(title))
                    {
                        context.Response.Write("增值服务标题不能为空!");
                        return;
                    }
                    if (!string.IsNullOrEmpty(context.Request["Price"]))
                    {
                        try
                        {
                            price = int.Parse(context.Request["Price"]);
                        }
                        catch (Exception)
                        {
                            context.Response.Write("价格必须为整数");
                            return;
                            throw;
                        }

                    }
                    else
                    {
                        context.Response.Write("价格不能为空!");
                        return;
                    }
                    Model.ExtraService service = new Model.ExtraService();
                    service.ServiceID = serviceID;
                    service.Title = title;
                    service.Price = price;

                    if (BLL.ExtraServiceBLL.Instance.AddExtraService(service))
                    {
                        context.Response.Write("100");
                        return;
                    }
                    else
                    {
                        context.Response.Write("该增值信息已存在 ！");
                        return;
                    }
                }
                else
                {
                    context.Response.Write("增值服务功能已关闭！");
                }
            } 
            catch (Exception)
            {
                throw;
            }
        }
        //收藏和取消收藏
        private void SetFavorite(HttpContext context)
        {

            try
            {
                Model.Favorite fav = new Model.Favorite();
                fav.ServiceID = context.Request["ServiceID"];
                fav.UserID = context.Request["UserID"];
                if (context.Request["Command"] == "Save")
                {
                    if (!BLL.FavoriteBLL.Instance.Contain(fav.ServiceID, fav.UserID))
                    {
                        fav.FavoriteID = Guid.NewGuid().ToString();
                        BLL.FavoriteBLL.Instance.Add(fav);
                    }
                }
                else if (context.Request["Command"].ToString() == "UnSave")
                {
                    BLL.FavoriteBLL.Instance.Delete(fav);
                }
                context.Response.Write(BLL.FavoriteBLL.Instance.GetSFavCountByServiceId(fav.ServiceID));

            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message);
                throw;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}